package com.vtech.ws.commons;

import org.apache.commons.io.FileUtils;

import javax.activation.DataHandler;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * TODO
 *
 * @author houzhiwei
 * @date 2018/9/11 22:31
 */
public class FileUploadHandler {

    /**
     * handle web service file upload
     *
     * @param upload  upload FileEntity
     * @param saveDir directory to save upload file
     * @return full path of saved upload file
     * @throws IOException
     */
    public static String upload(FileEntity upload, String saveDir) throws IOException {
        DataHandler file = upload.getFile();
        InputStream ins = new BufferedInputStream(file.getInputStream(),8192);
        String saveFile = saveDir + File.separator + upload.getName() + "." + upload.getExtension();
        File save = new File(saveFile);
        FileUtils.copyInputStreamToFile(ins, save);
        return saveFile;
    }
}
